import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"
import { getBanners } from "../api"
import type { Banner } from "../api/type"
interface RecommendState {
	banners: Banner[]
}

const initialState: RecommendState = {
	banners: [],
}

// 获取轮播图列表异步action
export const fetchBannersAction = createAsyncThunk("banners", async () => {
	const res = await getBanners()
	return res.banners
})
/* const fetchBannersAction = createAsyncThunk("banners", async () => {
	try {
		const res = await getBanners()
		return res.banners
	} catch (error) {
		console.log(error)
	}
}) */

const recommendSlice = createSlice({
	name: "recommend",
	initialState,
	reducers: {},
	extraReducers: (builder) => {
		builder
			.addCase(fetchBannersAction.pending, () => {
				console.log("pendding")
			})
			.addCase(fetchBannersAction.fulfilled, (state, action) => {
				state.banners = action.payload as Banner[]
			})
			.addCase(fetchBannersAction.rejected, () => {
				console.log("rejected")
			})
	},
})
export default recommendSlice.reducer
